package com.java110.mall.goods.bmo.housekeepingServ.impl;

import com.java110.dto.file.FileRelDto;
import com.java110.dto.housekeeping.HousekeepingServDto;
import com.java110.dto.product.ProductDetailDto;
import com.java110.dto.product.ProductServKeywordDto;
import com.java110.mall.goods.bmo.housekeepingServ.IGetHousekeepingServBMO;
import com.java110.intf.cart.IHousekeepingServFeeInnerServiceSMO;
import com.java110.intf.system.IFileInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.goods.IHousekeepingServInnerServiceSMO;
import com.java110.intf.goods.IProductDetailInnerServiceSMO;
import com.java110.intf.goods.IProductServKeywordInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.cache.UrlCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getHousekeepingServBMOImpl")
public class GetHousekeepingServBMOImpl implements IGetHousekeepingServBMO {

    @Autowired
    private IHousekeepingServInnerServiceSMO housekeepingServInnerServiceSMOImpl;
    @Autowired
    private IFileInnerServiceSMO fileInnerServiceSMOImpl;
    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
    @Autowired
    private IProductServKeywordInnerServiceSMO productServKeywordInnerServiceSMOImpl;
    @Autowired
    private IProductDetailInnerServiceSMO productDetailInnerServiceSMOImpl;
    @Autowired
    private IHousekeepingServFeeInnerServiceSMO housekeepingServFeeInnerServiceSMOImpl;

    /**
     * @param housekeepingServDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(HousekeepingServDto housekeepingServDto) {


        int count = housekeepingServInnerServiceSMOImpl.queryHousekeepingServsCount(housekeepingServDto);

        List<HousekeepingServDto> housekeepingServDtos = null;
        if (count > 0) {
            housekeepingServDtos = housekeepingServInnerServiceSMOImpl.queryHousekeepingServs(housekeepingServDto);
        } else {
            housekeepingServDtos = new ArrayList<>();
        }


        if (!ListUtil.isNull(housekeepingServDtos)) {
            String mallApp = UrlCache.getMallAppUrl();
            mallApp = mallApp.endsWith("/") ? mallApp : mallApp + "/";

            String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");

            imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");

            for (HousekeepingServDto housekeepingServ : housekeepingServDtos) {
                housekeepingServ.setPageUrl(mallApp + "#/pages/homemaking/detail?servId=" + housekeepingServ.getServId() + "&shopId=" + housekeepingServ.getShopId());

                //查询服务默认费用
                housekeepingServ.setDefaultFee(housekeepingServFeeInnerServiceSMOImpl.queryHousekeepingServDefaultFee(housekeepingServ.getShopId(), housekeepingServ.getServId()));

                //服务详情
                ProductDetailDto productDetailDto = new ProductDetailDto();
                productDetailDto.setShopId(housekeepingServ.getShopId());
                productDetailDto.setProductId(housekeepingServ.getServId());
                List<ProductDetailDto> productDetailDtos = productDetailInnerServiceSMOImpl.queryProductDetails(productDetailDto);
                if (!ListUtil.isNull(productDetailDtos)) {
                    ProductDetailDto productDetail = productDetailDtos.get(0);
                    housekeepingServ.setContext(productDetail.getContent());
                }
                String coverPhoto = doGetRentingFile(housekeepingServ, FileRelDto.REL_TYPE_CD_GOODS_COVER);
                if (!StringUtil.isEmpty(coverPhoto) && !coverPhoto.startsWith("http")) {
                    coverPhoto = imgUrl + coverPhoto;
                }
                //封面图
                housekeepingServ.setCoverPhoto(coverPhoto);


                //轮播图
                housekeepingServ.setCarouselFigurePhoto(doGetRentingFiles(imgUrl, housekeepingServ, FileRelDto.REL_TYPE_CD_GOODS_CAROUSEL_FIGURE));
                //关键字
                housekeepingServ.setKeyword(doGetkeywords(housekeepingServ));
            }
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) housekeepingServDto.getRow()), count, housekeepingServDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }


    /**
     * @param housekeepingServDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> getSearch(HousekeepingServDto housekeepingServDto) {
        int count = housekeepingServInnerServiceSMOImpl.queryHousekeepingServsCount(housekeepingServDto);
        List<HousekeepingServDto> housekeepingServDtos = null;
        if (count > 0) {
            housekeepingServDtos = housekeepingServInnerServiceSMOImpl.searchHousekeepingServs(housekeepingServDto);
        } else {
            housekeepingServDtos = new ArrayList<>();
        }

        if (!ListUtil.isNull(housekeepingServDtos)) {
            String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");
            imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
            for (HousekeepingServDto housekeepingServ : housekeepingServDtos) {
                //查询服务默认费用
                housekeepingServ.setDefaultFee(housekeepingServFeeInnerServiceSMOImpl.queryHousekeepingServDefaultFee(housekeepingServ.getShopId(), housekeepingServ.getServId()));
                //服务详情
                ProductDetailDto productDetailDto = new ProductDetailDto();
                productDetailDto.setShopId(housekeepingServ.getShopId());
                productDetailDto.setProductId(housekeepingServ.getServId());
                List<ProductDetailDto> productDetailDtos = productDetailInnerServiceSMOImpl.queryProductDetails(productDetailDto);
                if (!ListUtil.isNull(productDetailDtos)) {
                    ProductDetailDto productDetail = productDetailDtos.get(0);
                    housekeepingServ.setContext(productDetail.getContent());
                }
                String coverPhoto = doGetRentingFile(housekeepingServ, FileRelDto.REL_TYPE_CD_GOODS_COVER);
                if (!StringUtil.isEmpty(coverPhoto)) {
                    coverPhoto = imgUrl + coverPhoto;
                }
                //封面图
                housekeepingServ.setCoverPhoto(coverPhoto);
                //轮播图
                //housekeepingServ.setCarouselFigurePhoto(doGetRentingFiles(imgUrl, housekeepingServ, FileRelDto.REL_TYPE_CD_GOODS_CAROUSEL_FIGURE));
                //关键字
                //housekeepingServ.setKeyword(doGetkeywords(housekeepingServ));
            }
        }
        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) housekeepingServDto.getRow()), count, housekeepingServDtos);
        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        return responseEntity;
    }


    /**
     * 保存房屋租赁 图片
     *
     * @param housekeepingServDto
     */
    private String doGetRentingFile(HousekeepingServDto housekeepingServDto, String fileTypeCd) {

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjId(housekeepingServDto.getServId());
        fileRelDto.setRelTypeCd(fileTypeCd);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
        FileRelDto fileRel = null;
        if (!ListUtil.isNull(fileRelDtos)) {
            fileRel = fileRelDtos.get(0);
            return fileRel.getFileSaveName();
        }
        return "";
    }

    /**
     * 保存房屋租赁 图片
     *
     * @param housekeepingServDto
     */
    private List<String> doGetRentingFiles(String imgUrl, HousekeepingServDto housekeepingServDto, String fileTypeCd) {

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjId(housekeepingServDto.getServId());
        fileRelDto.setRelTypeCd(fileTypeCd);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
        List<String> fileRelName = new ArrayList<String>();
        if (ListUtil.isNull(fileRelDtos)) {
            return fileRelName;
        }

        for (FileRelDto filerels : fileRelDtos) {
            if (StringUtil.isEmpty(filerels.getFileSaveName())) {
                continue;
            }
            if (filerels.getFileSaveName().startsWith("http")) {
                fileRelName.add(filerels.getFileSaveName());
                continue;
            }
            fileRelName.add(imgUrl + filerels.getFileSaveName());
        }

        return fileRelName;
    }

    /**
     * 保存房屋租赁 图片
     *
     * @param housekeepingServDto
     */
    private String doGetkeywords(HousekeepingServDto housekeepingServDto) {

        ProductServKeywordDto servKeywordDtos = new ProductServKeywordDto();
        servKeywordDtos.setObjId(housekeepingServDto.getServId());
        servKeywordDtos.setShopId(housekeepingServDto.getShopId());
        String keywords = "";
        List<ProductServKeywordDto> serkeywords = productServKeywordInnerServiceSMOImpl.queryProductServKeywords(servKeywordDtos);
        if (ListUtil.isNull(serkeywords)) {
            return keywords;
        }
        for (ProductServKeywordDto servKeywordDto : serkeywords) {
            keywords = keywords + ";" + servKeywordDto.getKeywordName();
        }
        keywords = keywords.substring(1, keywords.length());


        return keywords;
    }

}
